package com.aidex.biz.smy.utils;
import org.apache.commons.codec.binary.Base64;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.*;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;

public class Test {
    //指定加密算法为RSA
    private static String ALGORITHM = "RSA";
    //指定key的大小
    private static int KEYSIZE = 1024;
    //指定公钥存放文件和私钥存放文件
    private static String PUBLIC_KEY_FILE = "public.key";
    private static String PRIVATE_KEY_FILE = "private.key";

    //生成公钥和私钥并分别存放在文件中
    private static void generateKeyPair() throws Exception{
        //生成密钥对
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
        kpg.initialize(KEYSIZE, new SecureRandom());
        KeyPair kp = kpg.generateKeyPair();
        //通过密钥对分别得到公钥和私钥
        Key publicKey = kp.getPublic();
        Key privateKey = kp.getPrivate();
        //将生成的密钥写入文件
        ObjectOutputStream output1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
        ObjectOutputStream output2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));
        output1.writeObject(publicKey);
        output2.writeObject(privateKey);
        output1.close();
        output2.close();
    }

    //RSA  公钥加密方法
    public static String encrypt(String source, String publicKeyFile) throws Exception {
        //读出文件中的公钥对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(publicKeyFile));
        Key key = (Key) ois.readObject();
        ois.close();
        //得到Cipher对象来实现对源数据的RSA加密
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] b = source.getBytes();
        String cryptograph = Base64Utils.encode(cipher.doFinal(b));
        return cryptograph;
    }

    //RSA  私钥解密方法
    public static String decrypt(String cryptograph, String privateKeyFile) throws Exception {
        //读出文件中的私钥对象
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(privateKeyFile));
        Key key = (Key) input.readObject();
        //  System.out.println("私钥为："+key.toString());
        input.close();
        //对已经加密的数据进行RSA解密
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] b1 = Base64Utils.decode(cryptograph);
        //执行解密操作
        byte[] b = cipher.doFinal(b1);
        String source = new String(b);
        return source;
    }

    //调用方法举例
    public static void main(String[] args) {
        System.out.println(generateRsaKey(1024));
    }
    private static final int DEFAULT_RSA_KEY_SIZE = 2048;

    private static final String KEY_ALGORITHM = "RSA";

    private static final String SIGNATURE_ALGORITHM = "MD5withRSA";

    public static Map<String,String> generateRsaKey(int keySize) {
        Map<String,String> result = new HashMap<>(2);
        try {
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

            // 初始化密钥对生成器，密钥大小为1024 2048位
            keyPairGen.initialize(keySize, new SecureRandom());

            // 生成一个密钥对，保存在keyPair中
            KeyPair keyPair = keyPairGen.generateKeyPair();

            // 得到公钥字符串
            result.put("publicKey", new String(Base64.encodeBase64(keyPair.getPublic().getEncoded())));

            // 得到私钥字符串
            result.put("privateKey", new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded())));

        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        return result;
    }


}
